/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package servlet.edt;
import beans.*;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import src.*;
import bdd.*;
import bdd.daos.impl.*;
import java.util.*;


/**
 *
 * @author cyhaberl
 */
public class consultation extends HttpServlet {
   
    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
			int promo = -1;
			int forma = -1;
			if(request.getParameter("promotion") == null || request.getParameter("formation") == null)
			{
				java.lang.System.exit(0);
			}
			else
			{
				promo = (request.getParameter("promotion") == null) ? -1 : Integer.parseInt(request.getParameter("promotion"));
				forma = (request.getParameter("formation") == null) ? -1 : Integer.parseInt(request.getParameter("formation"));
			}
			Calendar dateDuJour = Calendar.getInstance(); // date du jour
			int jour = (request.getParameter("jour") == null) ? dateDuJour.get(Calendar.DATE) : Integer.parseInt(request.getParameter("jour"));
			int mois = (request.getParameter("mois") == null) ? dateDuJour.get(Calendar.MONTH) : Integer.parseInt(request.getParameter("mois"));
			int annee = (request.getParameter("annee") == null) ? dateDuJour.get(Calendar.YEAR) : Integer.parseInt(request.getParameter("annee"));
			String planning = (request.getParameter("planning") == null) ? "semaine" : request.getParameter("planning");
			int jourF = (request.getParameter("jourF") == null) ? dateDuJour.get(Calendar.DATE) : Integer.parseInt(request.getParameter("jourF"));
			int moisF = (request.getParameter("moisF") == null) ? dateDuJour.get(Calendar.MONTH) : Integer.parseInt(request.getParameter("moisF"));
			int anneeF = (request.getParameter("anneeF") == null) ? dateDuJour.get(Calendar.YEAR) : Integer.parseInt(request.getParameter("anneeF"));

            out.println("<html>");
            out.println("<head>");
            out.println("<title>Consultation</title>");
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>Affichage de l'emploi du temps :</h1>");
            out.println("<a href='./consultation?promotion="+promo+"&formation="+forma+"&planning=jour&jour="+jour+"&mois="+mois+"&annee="+annee+"'>par jour</a>");
			out.println("<a href='./consultation?promotion="+promo+"&formation="+forma+"&planning=semaine&jour="+jour+"&mois="+mois+"&annee="+annee+"'>par semaine</a>");
			out.println("<a href='./consultation?promotion="+promo+"&formation="+forma+"&planning=mois&jour="+jour+"&mois="+mois+"&annee="+annee+"'>par mois</a>");
            out.println("<br>");

			if(planning.equals("semaine"))
			{
				 PlanningSemaine PS = new PlanningSemaine(jour, mois, annee, forma, promo);
				 out.println(PS.afficherSemaine());
			}
			else
			{
				if(planning.equals("mois"))
				{
					//FAIRE LA RECHERCHE DERNIER JOUR MOIS
					PlanningPeriode PP = new PlanningPeriode(forma, promo, 01, mois, annee, 31, mois, annee);
					out.println(PP.afficherPeriode());
				}
				else
				{
					if(planning.equals("jour"))
					{
						PlanningJour PJ = new PlanningJour(jour, mois, annee, forma, promo);
						out.println(PJ.afficherJour());
					}
					else
					{
						if(planning.equals("periode"))
						{
							//AFFICHER MENU DEROULANT
							
						}
						else
						{
							if(planning.equals("periodeChoisie"))
							{
								PlanningJour PJ = new PlanningJour(jour, mois, annee, forma, promo);
							}
						}
					}
				}
			}
            out.println("</body>");
            out.println("</html>");
        }
		catch(Exception e)
		{
			out.print(e.getMessage());
		}
		finally {
            out.close();
        }
    } 

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    } 

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {

        processRequest(request, response);


    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
